package com.pro.service.imple;

import com.google.gson.Gson;
import com.pro.common.GuavaCommon;
import com.pro.mapper.JsPeopleListMapper;
import com.pro.pojo.*;
import com.pro.service.JsPeopleListService;
import com.pro.utils.GuavaUtil;
import com.pro.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * ClassName:JsPeopleListServiceImple
 * Package:com.pro.service.imple
 * Description:
 *
 * @Date:2021/11/15 16:05
 * @Author: XJR
 */
@Service
public class JsPeopleListServiceImple implements JsPeopleListService {

    @Autowired
    private JsPeopleListMapper mapper;

    @Autowired
    private RedisCache redisCache;

    private Gson gson = new Gson();

    //查看部门名称
    public List<JsDept> selectJsDeptName(){
        try {
            Object obj = GuavaUtil.getTenCacheBuild().get(String.valueOf(GuavaCommon.Dept), () -> {
                List<JsDept> depts = mapper.selectJsDeptName();
                JsDept js = new JsDept();
                js.setId(0);
                js.setParentId(-1);
                js.setDeptName("所有部门");
                depts.add(js);
                return depts;
            });
            if (obj instanceof List){
                List<JsDept> list = (List<JsDept>)obj;
                return list;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    };

    //查询后台用户信息
    public List<JsUser> selectJsUser(String account, String trueName, String deptName, String registerDate,Integer limit,Integer page){
        page = (page-1)*limit;
        List<JsUser> jsUsers = mapper.selectJsUser(account, trueName, deptName, registerDate,limit,page);
        redisCache.setCacheObject("JsPeopleListSelectJsUser",gson.toJson(jsUsers));
        return jsUsers;
    };
    public Integer countJsUser(String account, String trueName, String deptName, String registerDate){
        Integer integer = mapper.countJsUser(account, trueName, deptName, registerDate);
        redisCache.setCacheObject("JsPeopleListCountJsUser",integer);
        return integer;
    };

    //更改账号状态
    public Boolean updateStatus(JsUser jsUser){
        Boolean aBoolean = mapper.updateStatus(jsUser);
        return aBoolean;
    };
    //将解封后的账号添加到解封日志中
    public Boolean insertUnseal(Integer userId,Integer type,String unsealDate){
        Boolean aBoolean = mapper.insertUnseal(userId, type, unsealDate);
        return aBoolean;
    };
    //解封后封停日志封停结束时间改为当前的时间
    public Boolean updateUserEndDate(Integer id,Integer userId,String endDate){
        Boolean aBoolean = mapper.updateUserEndDate(id, userId, endDate);
        return aBoolean;
    };
    //高级检索
    public List<JsUser> selectSearchJsUser(String email, String phone, String idCard, String status, String sex, String enabled,Integer limit,Integer page){
        page = (page-1)*limit;
        List<JsUser> jsUsers = mapper.selectSearchJsUser(email, phone, idCard, status, sex, enabled,limit,page);
        return jsUsers;
    };
    public Integer countSearchJsUser(String email, String phone, String idCard, String status, String sex, String enabled){
        Integer integer = mapper.countSearchJsUser(email, phone, idCard, status, sex, enabled);
        return integer;
    };

    //通过id查询用户数据详情
    public JsUser selectJsUserById(Integer id){
        JsUser jsUser = mapper.selectJsUserById(id);
        return jsUser;
    };
    public Integer countJsUserById(Integer id){
        Integer integer = mapper.countJsUserById(id);
        return integer;
    };

    //通过id查看用户的权限
    public List<JsUser> selectUserSettings(Integer userId){
        List<JsUser> jsUsers = mapper.selectUserSettings(userId);
        return jsUsers;
    };

    //更改后台用户账号状态
    public Boolean updateUserStatus(Integer userId,Integer status,Integer enabled){
        Boolean aBoolean = mapper.updateUserStatus(userId, status,enabled);
        return aBoolean;
    };
    //对后台用户账户进行封停
    public Boolean insertUserClosure(JsClosure jsClosure){
        Boolean aBoolean = mapper.insertUserClosure(jsClosure);
        return aBoolean;
    };
    //对后台用户账户进行编辑
    public Boolean updateUser(JsUser jsUser){
        Boolean aBoolean = mapper.updateUser(jsUser);
        return aBoolean;
    };

    //添加后台管理人员
    public Boolean addUser(JsUser jsUser){
        Boolean aBoolean = mapper.addUser(jsUser);
        return aBoolean;
    };
    //添加后台权限
    public Boolean addUserStatus(List<JsUser> jsUser){
        Boolean aBoolean = mapper.addUserStatus(jsUser);
        return false;
    };

    //删除后台权限
    public Boolean delUserStatus(Integer userId){
        Boolean aBoolean = mapper.delUserStatus(userId);
        return aBoolean;
    };
}
